package com.example.gulimall.product.controller;

import java.util.Arrays;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.example.gulimall.product.entity.BrandEntity;
import com.example.gulimall.product.vo.BrandRespVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import com.example.gulimall.product.entity.CategoryBrandRelationEntity;
import com.example.gulimall.product.service.CategoryBrandRelationService;
import com.example.gulimall.common.utils.PageUtils;
import com.example.gulimall.common.utils.R;



/**
 * 品牌分类关联
 *
 * @author xie
 * @email 2949459108@gmail.com
 * @date 2024-01-18 21:07:07
 */
@RestController
@RequestMapping("product/categorybrandrelation")
public class CategoryBrandRelationController {
    @Autowired
    private CategoryBrandRelationService categoryBrandRelationService;

    /**
     * 列表
     */
    @RequestMapping("/list")
    public R list(@RequestParam Map<String, Object> params){
        PageUtils page = categoryBrandRelationService.queryPage(params);

        return R.ok().put("page", page);
    }

    /**
     * 获取当前品牌关联的所有分类
     */
    @RequestMapping("/catelog/list")
    public R list(@RequestParam (value = "brandId")Long brandId){
        QueryWrapper<CategoryBrandRelationEntity> eq = new QueryWrapper<CategoryBrandRelationEntity>();
        List<CategoryBrandRelationEntity> data = categoryBrandRelationService.list(eq.lambda().eq(CategoryBrandRelationEntity::getBrandId, brandId));
        return R.ok().put("data", data);
    }

    /**
     * 信息
     */
    @RequestMapping("/info/{id}")
    public R info(@PathVariable("id") Long id){
		CategoryBrandRelationEntity categoryBrandRelation = categoryBrandRelationService.getById(id);

        return R.ok().put("categoryBrandRelation", categoryBrandRelation);
    }

    /**
     * 保存
     */
    @RequestMapping("/save")
    public R save(@RequestBody CategoryBrandRelationEntity categoryBrandRelation){
		categoryBrandRelationService.saveDetail(categoryBrandRelation);

        return R.ok();
    }

    /**
     * 修改
     */
    @RequestMapping("/update")
    public R update(@RequestBody CategoryBrandRelationEntity categoryBrandRelation){
		categoryBrandRelationService.updateById(categoryBrandRelation);
        return R.ok();
    }

    /**
     * 删除
     */
    @RequestMapping("/delete")
    public R delete(@RequestBody Long[] ids){
		categoryBrandRelationService.removeByIds(Arrays.asList(ids));

        return R.ok();
    }
    @GetMapping("/brands/list")
    public R relationBrand(@RequestParam(value = "catId")Long catId){
        List<BrandEntity> brandEntityList=categoryBrandRelationService.getRelationBrandByCatId(catId);
        List<BrandRespVo> brandRespVoList = brandEntityList.stream().map((brand -> {
            BrandRespVo brandRespVo = new BrandRespVo();
            brandRespVo.setBrandId(brand.getBrandId());
            brandRespVo.setBrandName(brand.getName());
            return brandRespVo;
        })).collect(Collectors.toList());
        return R.ok().put("data",brandRespVoList);
    }

}
